﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Diagnostics;
using System.IO;

namespace CFScript.Tasks
{
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Rename : Task
    {
        public Rename()
        {
            this.name = "<rename>";
            this.parameterNames = new string[] { "fileFrom", "fileTo" };
            this.parameterExpandable = new bool[] { true, true };
            this.parameterRequired = new bool[] { true, true };
            this.parameters = new Dictionary<string, Parameter>();

            Debug.Assert(this.parameterNames.Length == this.parameterRequired.Length &&
                         this.parameterRequired.Length == this.parameterRequired.Length);
        }

        public override InstructionResult Execute(CFScript context)
        {
            InstructionResult result = new InstructionResult();
            result.ExecutedInstruction = this;

            string fileFrom = this.parameters["fileFrom"].GetValue(context);
            string fileTo = this.parameters["fileTo"].GetValue(context);
            bool sourceIsADirectory = false;

            try
            {
                // Let's rename the file

                FileInfo sourceFileInfo = new FileInfo(fileFrom);

                sourceIsADirectory = (sourceFileInfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory;

                if (sourceFileInfo.Exists)
                {
                    if (sourceIsADirectory) //--------------- It's a Directory ------------------
                    {
                        // We combine directory source name with final name
                        string sourceDirectory = Path.GetDirectoryName(fileFrom);
                        string dest = Path.Combine(sourceDirectory, fileTo);

                        if (fileFrom != dest)
                        {
                            if (Directory.Exists(dest))
                            {
                                result.Code = ResultCode.ERROR;
                                switch (context.Options.VerboseLevel)
                                {
                                    case VerboseMode.MINIMUM:
                                    case VerboseMode.ALL:
                                        result.Message = string.Format("Already exists a folder with the 'fileTo' name: {0}", fileTo);
                                        break;
                                }
                            }
                            else
                                Directory.Move(fileFrom, fileTo);
                        }
                        else
                        {
                            result.Code = ResultCode.ERROR;
                            switch (context.Options.VerboseLevel)
                            {
                                case VerboseMode.MINIMUM:
                                case VerboseMode.ALL:
                                    result.Message = string.Format("Attributes 'fileFrom' and 'fileTo' must be different");
                                    break;
                            }
                        }
                    }
                    else //--------------- It's a File ------------------
                    {
                        // We combine directory source name with final name
                        string sourceDirectory = Path.GetDirectoryName(fileFrom);
                        string dest = Path.Combine(sourceDirectory, fileTo);

                        if (fileFrom != dest)
                        {
                            if (File.Exists(dest))
                            {
                                result.Code = ResultCode.ERROR;
                                switch (context.Options.VerboseLevel)
                                {
                                    case VerboseMode.MINIMUM:
                                    case VerboseMode.ALL:
                                        result.Message = string.Format("Already exists a file with the 'fileTo' name: {0}", fileTo);
                                        break;
                                }
                            }
                            else
                                File.Move(fileFrom, fileTo);
                        }
                        else
                        {
                            result.Code = ResultCode.ERROR;
                            switch (context.Options.VerboseLevel)
                            {
                                case VerboseMode.MINIMUM:
                                case VerboseMode.ALL:
                                    result.Message = string.Format("Attributes 'fileFrom' and 'fileTo' must be different");
                                    break;
                            }
                        }

                    }
                    result.Code = ResultCode.OK;
                }
                else
                {
                    // File not present. Can't rename
                    result.Code = ResultCode.WARN;
                    switch (context.Options.VerboseLevel)
                    {
                        case VerboseMode.MINIMUM:
                        case VerboseMode.ALL:
                            result.Message = string.Format("Source {0} {1} not found", sourceIsADirectory?"path":"file",fileFrom);
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                result.Code = ResultCode.ERROR;
                result.Message = ex.Message;
            }

            result.BreakExecution = result.Code == ResultCode.ERROR && this.options.OnError == OnErrorAction.BREAK;

            return result;
        }
    }
}
